import { Module } from 'vuex'
import { IDashboardState } from './types'
import { IRootState } from '../types'
import { getCategoryGoodsCount, getCategoryGoodsFavor, getCategoryGoodsSale, getAddressGoodsSale } from '@/service/main/analysis/dashboard'

const dashboardModule: Module<IDashboardState, IRootState> = {
  namespaced: true,
  state() {
    return {
      categoryGoodsCount: [],
      categoryGoodsFavor: [],
      categoryGoodsSale: [],
      addressGoodsSale: []
    }
  },
  mutations: {
    saveCategoryGoodsCount(state, paylod: any) {
      state.categoryGoodsCount = paylod
    },
    saveCategoryGoodsFavor(state, paylod: any) {
      state.categoryGoodsFavor = paylod
    },
    saveCategoryGoodsSale(state, paylod: any) {
      state.categoryGoodsSale = paylod
    },
    saveAddressGoodsSale(state, paylod: any) {
      state.addressGoodsSale = paylod
    },
  },
  actions: {
    async getDashboardDataActions({ commit }) {
      // 请求数据
      const categoryGoodsCountResult = await getCategoryGoodsCount()
      const categoryGoodsFavorResult = await getCategoryGoodsFavor()
      const categoryGoodsSaleResult = await getCategoryGoodsSale()
      const addressGoodsSaleResult = await getAddressGoodsSale()
      // 保存数据
      commit('saveCategoryGoodsCount', categoryGoodsCountResult.data)
      commit('saveCategoryGoodsFavor', categoryGoodsFavorResult.data)
      commit('saveCategoryGoodsSale', categoryGoodsSaleResult.data)
      commit('saveAddressGoodsSale', addressGoodsSaleResult.data)
    }
  }
}

export default dashboardModule
